﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Spore.Utils
{
   public class WinMsgHelper
    {
       private static Dictionary<int, object> dic= new Dictionary<int,object>();
       static int key = 0;
       static object obj_lock = new object();
       public static void PostMsg(IntPtr receiver, int msg, object obj)
       {
           lock (obj_lock)
           {
               key++;
               dic.Add(key, obj);
               if (!PostMessage(receiver, msg, key, 0))
               {
                   dic.Remove(key);
               }
           }
       }

       public static void PostMsg(IntPtr receiver, int msg, object obj, int lParam)
       {
           lock (obj_lock)
           {
               key++;
               dic.Add(key, obj);
               if (!PostMessage(receiver, msg, key, lParam))
               {
                   dic.Remove(key);
               }
           }
       }

       public static T GetValue<T>(int key)
       {
           lock (obj_lock)
           {
               object value = dic[key];
               dic.Remove(key);
               return (T)value;
           }
       }

       public static object GetValue(int key)
       {

           lock (obj_lock)
           {
               object value = dic[key];
               dic.Remove(key);
               return value;
           }
       }

       [DllImport("user32.dll")]
       private static extern bool PostMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

       [DllImport("user32")]
       private static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
    }
}
